package homework1;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.util.HashMap;

public class MapperTest1 {
    ObjectMapper mapper = new ObjectMapper();


    @Test
    public void test01()throws  Exception{
        Student stu = new Student("小雪",18);
        String json = mapper.writeValueAsString(stu);
        System.out.println(json);

        Student student = mapper.readValue(json, Student.class);
        System.out.println(student);
    }

    @Test
    public void test02()throws  Exception{
        HashMap<String,String>map = new HashMap<>();
        map.put("小雪","美女");
        map.put("老妖","帅哥");
        String json = mapper.writeValueAsString(map);
        System.out.println(json);

        HashMap java = mapper.readValue(json, HashMap.class);
        System.out.println(java);
    }


    @Test
    public void test03()throws  Exception{
       HashMap<String,Student>map=new HashMap<>();
       map.put("海景房一套",new Student("老妖",1000));
        String json = mapper.writeValueAsString(map);
        System.out.println(json);

        HashMap<String,Student>map2 = mapper.readValue(json, new TypeReference<HashMap<String, Student>>() {});
        System.out.println(map2);
    }
}
